<!DOCTYPE html>
<html>
<head>
#include("../../_common/head.html")
<title>定时任务列表</title>
</head>
<body class="pear-container">
    <div class="layui-panel query-panel /*查询面板*/">
        <form class="layui-form layui-row layui-col-space16" lay-filter="query-form">
        	<div class="layui-col-xl2 layui-col-lg2 layui-col-md2 layui-col-sm3 layui-col-xs6">
				<div class="layui-input-wrap">
					<div class="layui-input-prefix">
						<i class="pear-icon pear-icon-text"></i>
					</div>
					<input type="text" name="word" value="#(word??)" placeholder="模糊查询" autocomplete="off" class="layui-input" lay-affix="clear">
				</div>
		    </div>
        	<div class="layui-col-xl2 layui-col-lg2 layui-col-md2 layui-col-sm3 layui-col-xs6">
				<div class="layui-input-wrap">
                    <select name="group">
                        <option value="">-- 任务分组 --</option>
                        #DICTS(type="job_group")
                        #for(item : dicts)
                        <option value="#(item.value)"#if(type?? == item.value) selected#end>#(item.label)</option>
                        #end
                    </select>
				</div>
		    </div>
            <div class="layui-col-xl2 layui-col-lg2 layui-col-md2 layui-col-sm3 layui-col-xs6">
                <button type="button" class="pear-btn pear-btn-sm pear-btn-primary query-form-search">
                    <i class="layui-icon layui-icon-search"></i> 搜索
                </button>
                <button type="button" class="pear-btn pear-btn-sm query-form-reset">
                    <i class="layui-icon layui-icon-refresh"></i> 重置
                </button>
            </div>
        </form>
    </div>
    <div class="layui-panel data-panel /*数据面板*/">
        <table id="data-table" lay-filter="data-table"></table>
    </div>

    <!-- 状态分组 -->
    <script type="text/html" id="bean-jobGroup">
    <!-- #DICTS(type="job_group")#for(item : dicts) -->
    #[[{{# if(d.group == ']]##(item.value)#[[') { }}]]#
        <span class="#(item.listClass)">#(item.label)</span>
    #[[{{# } }}]]#
    <!-- #end -->
    </script>
    
    <!-- 状态字段 -->
    <script type="text/html" id="bean-jobStatus">
    <input data-id= "{{ d.id }}" type="checkbox" lay-skin="switch" lay-filter="job_status" lay-text="启用|停用" #[[{{# if (d.status == "ON") { }}]]# checked #[[{{# } }}]]# value="{{ d.status }}">
    </script>
    
    <!-- 创建时间 -->
    <script type="text/html" id="bean-createdDatetime">
        {{layui.util.toDateString(d.createdDatetime, "yyyy-MM-dd HH:mm:ss")}}
    </script>

#authPermissions('monitor:job:query')
    #set(tableButtonsHtml = '<a class="pear-btn pear-btn-success pear-btn-xs" round lay-event="jobLog"><i class="layui-icon layui-icon-survey"></i>&nbsp;调度日志</a>') ### 自定义表头按钮
#end
#authPermissions('monitor:job:detail', 'monitor:job:detail')
    #set(recordButtonsHtml = '<a class="pear-btn pear-btn-success pear-btn-xs" plain id="more_{{d.id}}" data-id="{{d.id}}" data-name="{{d.name}}" data-group="{{d.group}}"><i class="layui-icon layui-icon-triangle-d"></i>&nbsp;更多</a>') ### 自定义单记录按钮
#end
#include("../../_common/data_table_template.html"
    , customTableButtons = tableButtonsHtml
    , customRecordButtons = recordButtonsHtml
)

#include("../../_common/foot.html")

<script type="text/javascript">
/*
 * 定义权限开关
 */
var RIGHTS = { NONAME: null // 无用，占个位
//#authPermissions('monitor:job:add')
    ,add: true
//#end
//#authPermissions('monitor:job:edit')
    ,edit: true
//#end
//#authPermissions('monitor:job:del')
    ,remove: true
    ,batchRemove: true
//#end
//#authPermissions('monitor:job:detail')
    ,detail: true
//#end

};

/*
 * 模块操作路径
 */
var MODULE_PATH = "#CTX()/admin/monitor/job/";

/*
 * 默认弹窗大小
 */
var POPUP_AREA = ['720px', '95%'];

/*
 * 数据表的OPTIONS
 */
var OPTIONS = {
    elem: '#data-table',
    url: MODULE_PATH + "query",
    method: "post",
    request: {pageName: 'pageNumber', limitName: 'pageSize' },
    initSort: {"field": "createdDatetime", "type": "desc"},
    page: true,
    height: 'full-95',
    toolbar: '#table-toolbar',
    defaultToolbar: [{title: "查询", layEvent: "toggle", icon: "layui-icon-search"}, 'filter'],
    size: 'sm',
    cols: [[
        {type: 'checkbox', align: 'center', width: 60},
        {field: 'name', align: 'left', minWidth: 150, width: '10%', sort: true, title: '任务名称'},
        {field: 'group', align: 'left', minWidth: 80, width: '10%', sort: true, title: '任务分组', templet: '#bean-jobGroup'},
        {field: 'target', align: 'left', minWidth: 150, width: '15%', title: '调用目标字符串'},
        {field: 'cron', align: 'left', minWidth: 150, width: '15%', title: '执行表达式'},
        {field: 'status', align: 'center', width: 100, sort: true, title: '任务状态', templet: '#bean-jobStatus'},
        {field: 'createdDatetime', align: 'center', width: 160, sort: true, title: '创建时间', templet: '#bean-createdDatetime'},
        {toolbar: '#record-toolbar', align: 'left', minWidth: 240, title: '操作'}
    ]]
};
</script>

<script src="#CTX()/admin/js/query_form.js?_v=#VERSION()"></script>
<script src="#CTX()/admin/js/data_table.js?_v=#VERSION()"></script>

<script type="text/javascript">
/*
 * Layui相关脚本
 */
layui.use(['jquery', 'common', 'form', 'dropdown'], () => {
    var $ = layui.jquery, form = layui.form, common = layui.common;
    var datatable = layui.table; // 数据表格
	var dropdown = layui.dropdown;
	
	OPTIONS.done = (result, curr, count) => {
        for (var i = 0; i < result.data.length; i++) {
            var data = [];
            //#authPermissions('system:role:assignPermission')
            data.push({title: "<i class='pear-icon pear-icon-operation'></i>&nbsp;执行一次", menuId: 100, id: result.data[i].id});
            //#end
            //#authPermissions('monitor:job:query')
            data.push({title: "<i class='pear-icon pear-icon-survey'></i>&nbsp;调度日志", menuId: 101, id: result.data[i].id, name: result.data[i].name, group: result.data[i].group});
            //#end
            
            dropdown.render({
                elem: '#more_' + result.data[i].id,
                data: data,
                id: '#more_' + result.data[i].id,
                click: function(obj) {
                    if (obj.menuId == 100) {
                        // 执行一次
                        window.dataTable.runOnce(obj.id);
                    }
                    if (obj.menuId == 101) {
                        // 调度日志
                        window.dataTable.jobLog(obj.name, obj.group);
                    }
                }
            });
        }
    };
    
    /*
     * 监听自定义事件
     */
    window.dataTable.handleEvent = (event, obj) => {
        if (event === 'jobLog') {
            window.dataTable.jobLog("", "");
        }
    }
    
    /*
     * 执行一次任务
     */
    window.dataTable.runOnce = function(obj) {
        layer.confirm("确认要立即执行一次任务吗？", {
            icon: 3,
            title: '确认'
        }, function(index) {
            layer.close(index);
            $.ajax({
                url: MODULE_PATH + "runOnce?id=" + obj,
                beforeSend : function(){layer.load();}, // 防止重复操作
                complete : function(){layer.closeAll('loading');}, //解除防重复操作
                success: function(result) {
                    if (result.code == 200) {
                        layer.alert("操作成功！", {icon: 1});
                    } else {
                        if(result.description){
                            // Ajax调用成功，但后台返回失败信息
                            layer.alert(result.description, {icon: 5});
                        } else {
                            layer.alert("操作失败！", {icon: 2});
                        }
                    }
                }
            });
        });
    };
    
    /*
     * 查看调度日志
     */
    window.dataTable.jobLog = function(name, group) {
        top.layui.admin.closeTab("调度日志")
        top.layui.admin.jump("调度日志", "<i class='pear-icon pear-icon-survey'></i>&nbsp; 调度日志" , '#CTX()/admin/monitor/jobLog?word=' + name + "&group=" + group, false);
    }
    
    /*
     * 切换任务执行状态
     */
    form.on('switch(job_status)', function(data){
        var value = data.elem.checked;
        var id = $(data.elem).attr("data-id");
        var tips = value ? "确定要 [启动] 定时任务？" : "确定要 [停用] 定时任务？";
        layer.confirm(tips, {
            icon: 3,
            title: '提示'
        }, function(index) {
            layer.close(index);
            var loading = layer.load();
            $.ajax({
                url : MODULE_PATH + 'changeStatus?id=' + id + "&status=" +  value,
                type : "get",
                beforeSend : function(){layer.load();}, // 防止重复操作
                complete : function(){layer.closeAll('loading');}, // 解除防重复操作
                success : function(result){
                    if(result.code == 200){
                        layer.alert("操作成功！", {icon: 1});
                    } else {
                        data.elem.checked = result.data;
                        if(result.description){
                            layer.alert(result.description, {icon: 5});
                        } else {
                            layer.alert("操作失败！", {icon: 2});
                        }
                    }
                    form.render('checkbox');
                },
                error : function (e) {
                    data.elem.checked = !value;
                    layer.alert("执行失败：" + e.responseText, {icon: 2});
                    form.render('checkbox');
                }
            });
        }, function(index) {
            data.elem.checked = !value;
            form.render('checkbox');
        });
    });
    
    /*
     * 页面加载完成时加载表格数据
     */
    $(document).ready(() => {
        window.dataTable.search();
    });
    
});
</script>

</body>
</html>
